package com.hermes.test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.hermes.action.subject.SubjectSwitch;
import com.hermes.biz.common.model.result.UserResultDO;
import com.hermes.biz.common.model.user.UserDO;
import com.hermes.biz.manager.user.UserOperateManager;
import com.hermes.common.action.AsynBaseAction;
import com.hermes.common.util.ParameterParserUtil;

public class ChangePassword extends AsynBaseAction{
	public UserOperateManager userOperateManager;

	String userid="";
	String oldpsw="";
	String newpsw="";
	public UserDO userDO;
	private Logger log = Logger.getLogger(SubjectSwitch.class);
	@Override
	public String execute() throws Exception {
		//判定是否登录，以后此逻辑独立出来以公用
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpSession session = request.getSession();
		HttpServletResponse response = ServletActionContext.getResponse();
		String senUserId = null;
		try{
			senUserId = (String) session.getAttribute("userId");
		}catch(Exception e){
			log.error("获取session.userid失败");
		}
		if(null!=senUserId&&senUserId.equals(userid)){
			userid=ParameterParserUtil.getParameter("userid");
			oldpsw=ParameterParserUtil.getParameter("oldpsw");
			//check old password
			UserResultDO resultDO = userOperateManager.getSingleUserByUserId(Long.valueOf(userid));
			userDO = resultDO.getModule();
			if(null!=userDO){
				if(!userDO.getUserPassword().equals(oldpsw)){
					return ERROR;
				}
			}
			newpsw=ParameterParserUtil.getParameter("newpsw");
			//update password
			userOperateManager.updateUserPassword(userDO);
		}else{
			response.sendRedirect("./index.htm");
			return ERROR;
		}
		return SUCCESS;
	}

	//getters and setters
	public UserOperateManager getUserOperateManager() {
		return userOperateManager;
	}
	public void setUserOperateManager(UserOperateManager userOperateManager) {
		this.userOperateManager = userOperateManager;
	}
}
